//
//  FourthViewController.m
//  真·豆瓣半完全体
//
//  Created by dllo on 15/12/31.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "FourthViewController.h"
#import "FourViewController4_1.h"
#import "FourCollectViewController.h"
#import "FourActivityViewController.h"
#import "MyDataBaseHandle.h"
#import "ActivityDataBaseHandle.h"
#import <sqlite3.h>
@interface FourthViewController ()<UITableViewDataSource, UITableViewDelegate>


@property (nonatomic, retain) UITableView *tableView;


@end

@implementation FourthViewController
- (void)dealloc{
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:84/255.0 green:139/255.0 blue:156/255.0 alpha:1];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = @"我的";
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"登陆" style:UIBarButtonItemStylePlain target:self action:@selector(action:)];
    self.navigationItem.rightBarButtonItem = item;
    
    [self creatTableView];
}





#pragma mark - tableViewDelegate
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 10;
}
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    NSArray *arr = @[@"我的活动", @"我的收藏", @"清除缓存"];
    if (indexPath.row == 0) {
        cell.textLabel.text = arr[0];
        return cell;
    }
    else if (indexPath.row == 1){
        cell.textLabel.text = arr[1];
        return cell;
    }
    else if(indexPath.row == 2){
        cell.textLabel.text = arr[2];
        return cell;
    }
    else{
        return cell;
    }
    
}
/** tableView点击方法 */
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        FourActivityViewController *fourActivityVc = [[FourActivityViewController alloc] init];
        [self.navigationController pushViewController:fourActivityVc animated:YES];
    }
    if (indexPath.row == 1) {
        FourCollectViewController *fourCollectVc =[[FourCollectViewController alloc ]init];
        [self.navigationController pushViewController:fourCollectVc animated:YES];
    }
    if (indexPath.row == 2) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"注意" message:@"请问要清除哪个缓存" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"清除活动" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [[ActivityDataBaseHandle sharedActivityDataBaseHandle] deleteModel];
        }];
        UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"清除收藏" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [[MyDataBaseHandle sharedMyDataBaseHandle] deleteModel];
        }];
        UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        [alert addAction:action];
        [alert addAction:action2];
        [alert addAction:action3];
        [self presentViewController:alert animated:YES completion:^{
            
        }];
        
    }
}



/** 创建tableView */
- (void) creatTableView{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 375, 554) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView release];
    
    
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
}

/** item点击方法 */
- (void)action:(UIBarButtonItem *) item{
    FourViewController4_1 *fourVc = [[FourViewController4_1 alloc] init];
    fourVc.modalTransitionStyle = 0;
    fourVc.block = ^(NSString *str){
//        item.title = str;
        UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:str style:UIBarButtonItemStylePlain target:self action:@selector(action2:)];
        self.navigationItem.rightBarButtonItem = item2;

    };
    UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:fourVc];
    [self presentViewController:navi animated:YES completion:^{
        
    }];
    [fourVc release];
}
/** 注销登陆方法 */
- (void)action2:(UIBarButtonItem *) item{
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定要注销吗？" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            UIBarButtonItem *item3 = [[UIBarButtonItem alloc] initWithTitle:@"登陆" style:UIBarButtonItemStylePlain target:self action:@selector(action:)];
            self.navigationItem.rightBarButtonItem = item3;
        }];
        [alert addAction:action];
        [alert addAction:action2];
        [self presentViewController:alert animated:YES completion:^{
 
        }];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
